//
//  DizhiChoseView.m
//  NBProduct
//
//  Created by 张铁军 on 16/8/25.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "DizhiChoseView.h"

@implementation DizhiChoseView{
    UIView *_backView;
    UIToolbar * _toolbar;
    NSArray *_shujuAry;
    NSArray *_shiAry;
    NSArray *_quDataAry;
    NSMutableArray *_diZhiAry;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        _diZhiAry = [[NSMutableArray alloc]initWithArray:@[@"",@"",@""]];
        [self initView];
        
    }
    return self;
}

-(void)initView{
    self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    _backView = [[UIView alloc]initWithFrame:CGRectMake(0, self.height - 230 * _kscH,KScreenWidth, 230 * _kscH)];
    _backView.backgroundColor = [UIColor whiteColor];
  
    _toolbar=[[UIToolbar alloc]init];
    _toolbar.barTintColor=[UIColor whiteColor];
    _toolbar.frame=CGRectMake(0, 0, KScreenWidth, 30 * _kscH);
    
    UIBarButtonItem *item0=[[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(quXiaoclick) ];
    UIBarButtonItem *item2=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *item1=[[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(queDingClick)];
    
    _toolbar.items = @[item0, item2,item1];
    
    [_backView addSubview:_toolbar];
    
    _shiPicker = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 30 * _kscH, KScreenWidth, 200 * _kscH)];
    _shiPicker.delegate = self;
    _shiPicker.dataSource = self;
    _shiPicker.showsSelectionIndicator = YES;
    
    [_backView addSubview:_shiPicker];
    [self addSubview:_backView];
    [self requestShengData];
}

-(void)quXiaoclick{
    [self removeFromSuperview];
}

-(void)queDingClick{
    [self.delegate choseEndClicked:_diZhiAry];
    [self removeFromSuperview];
}

-(void)requestShengData{
    [NetWorking NEWjieKouQingQiu:DZ_shenShiQu ANDIP:IP And:nil anBlock:^(NSDictionary *dic, NSError *error) {
        if (!error) {
            
            NSLog(@"省份%@",[[[dic objectForKey:@"Response"]firstObject]objectForKey:@"regionName"] );
            if ([[dic objectForKey:Code]intValue] == 200) {
                _shujuAry = [dic objectForKey:@"Response"];
                
                if (_shujuAry == nil || _shujuAry.count == 0) {
                    _shujuAry = @[];
                    [_shiPicker reloadAllComponents];
                }else{
                    //                [_myTableVie reloadData];
                    [self requestShiDataWidth:[[_shujuAry firstObject]objectForKey:@"regionId"]];
                    [_diZhiAry replaceObjectAtIndex:0 withObject:[_shujuAry firstObject]];
                }
            }
        }
    }];
}

-(void)requestShiDataWidth:(NSString *)shenFenStr{
    [NetWorking NEWjieKouQingQiu:DZ_shenShiQu ANDIP:IP And:@{@"addressId":shenFenStr} anBlock:^(NSDictionary *dic, NSError *error) {
        if (!error) {
            NSLog(@"市区%@",[[[dic objectForKey:@"Response"]firstObject]objectForKey:@"regionName"] );
            if ([[dic objectForKey:Code]intValue] == 200) {
                _shiAry = [dic objectForKey:@"Response"];
                if (_shiAry == nil || _shiAry.count == 0) {
                    _shiAry = @[];
                     _quDataAry = @[];
                    [_shiPicker reloadAllComponents];
                   
                }else{
                    
                    [self requestQuDataWith:[[_shiAry firstObject]objectForKey:@"regionId"]];
                     [_diZhiAry replaceObjectAtIndex:1 withObject:[_shiAry firstObject]];
                }
            }
        }
    }];
}

-(void)requestQuDataWith:(NSString *)chengShiStr{
    [NetWorking NEWjieKouQingQiu:DZ_shenShiQu ANDIP:IP And:@{@"addressId":chengShiStr} anBlock:^(NSDictionary *dic, NSError *error) {
    
        if (!error) {
            
            NSLog(@"区域%@",[[[dic objectForKey:@"Response"]firstObject]objectForKey:@"regionName"] );
            if ([[dic objectForKey:Code]intValue] == 200) {
                _quDataAry = [dic objectForKey:@"Response"];
              [_diZhiAry replaceObjectAtIndex:2 withObject:[_quDataAry firstObject]];
                [_shiPicker reloadAllComponents];
            }
        }
    }];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return _shujuAry.count;
    }else if(component == 1){
        return _shiAry.count;
        
    }else{
        return _quDataAry.count;
    }
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    if (component == 0) {
        return [[_shujuAry objectAtIndex:row]objectForKey:@"regionName"] ;
    }else if(component == 1){
        return [[_shiAry objectAtIndex:row]objectForKey:@"regionName"] ;
        
    }else{
        return [[_quDataAry objectAtIndex:row]objectForKey:@"regionName"] ;
    }
    
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        if (_shujuAry.count > 0) {
           [pickerView selectRow:0 inComponent:1 animated:NO];
            [self requestShiDataWidth:[[_shujuAry objectAtIndex:row]objectForKey:@"regionId"]];
            [_diZhiAry replaceObjectAtIndex:0 withObject:[_shujuAry objectAtIndex:row]];
            
        }
    }else if(component == 1){
        if (_shiAry.count > 0) {
            
             [self requestQuDataWith:[[_shiAry objectAtIndex:row]objectForKey:@"regionId"]];
            [_diZhiAry replaceObjectAtIndex:1 withObject:[_shiAry objectAtIndex:row]];
        }
    }else{
         [_diZhiAry replaceObjectAtIndex:2 withObject:[_quDataAry objectAtIndex:row]];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
